﻿using DotNetCommon.Extensions;
using System;
using System.IO;
using System.Threading;

namespace DotNetCommonLoggerConsoleTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //SetBaseDirectory
            DotNetCommon.Logger.LoggerFactory.SetBaseDirectory("c:/commonlog");
            //SetOutDirectory
            DotNetCommon.Logger.LoggerFactory.SetOutDirectory(arg =>
            {
                if (arg.LogContext.Level == DotNetCommon.Logger.LoggerLevel.Error || arg.LogContext.Level == DotNetCommon.Logger.LoggerLevel.Critical)
                {
                    return Path.Combine(arg.BaseDir, $"errors/{DateTime.Now.ToString("err-yyyyMMdd")}.txt");
                }
                return arg.PreLogFile;
            });
            //SetCanOut
            DotNetCommon.Logger.LoggerFactory.SetCanOut(arg =>
            {
                return arg.Level >= DotNetCommon.Logger.LoggerLevel.Information;
            });

            //SetOutFormat
            DotNetCommon.Logger.LoggerFactory.SetOutFormat(arg =>
            {
                if (arg.LogContext.CategoryName.StartsWith("DotNetCommonLoggerConsoleTest"))
                {
                    return arg.LogContext.ToJson();
                }
                return arg.PreMessage;
            });

            var logger = DotNetCommon.Logger.LoggerFactory.CreateLogger<Program>();
            logger.LogTrace("LogTrace");
            logger.LogDebug("LogDebug");
            logger.LogInformation("LogInformation");
            logger.LogWarning("LogWarning");
            logger.LogError("LogError");
            logger.LogCritical("LogCritical");


            Thread.Sleep(1000);
            Console.WriteLine($"日志已输出...");
            Console.ReadLine();
        }
    }
}
